//
//  MTPartyTypeView.swift
//  MT
//
//  Created by ooops on 2020/5/29.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit

protocol MTPartyTypeViewDelegate: NSObjectProtocol {
    func typeSelectDidRemoveFromSuperView(value: String)
}

extension MTPartyTypeView: UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        if let v = touch.view, v.isDescendant(of: self.tableView) {
            return false
        }
        return true
    }
    
    //MARK: UITableViewDataSource
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 0 ? 2 : 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 1 {
            let cell: MTPartyCancelCell = tableView.dequeueReusableCell(withIdentifier: MTPartyCancelCell.reuseIdentity(), for: indexPath) as! MTPartyCancelCell
            return cell
        }
        switch indexPath.row {
        case 0:
            let cell: MTTypeExTableViewCell = tableView.dequeueReusableCell(withIdentifier: MTTypeExTableViewCell.reuseIdentity(), for: indexPath) as! MTTypeExTableViewCell
            cell.nameLabel.text = "商业任务"
            cell.iconImageView.image = UIImage(named: "icon_auth_buss")
            return cell
        case 1:
            let cell: MTTypeTableViewCell = tableView.dequeueReusableCell(withIdentifier: MTTypeTableViewCell.reuseIdentity(), for: indexPath) as! MTTypeTableViewCell
            cell.nameLabel.text = "个人任务"
            cell.iconImageView.image = UIImage(named: "icon_auth_personal")
            return cell
        default:
            let cell: MTPartyCancelCell = tableView.dequeueReusableCell(withIdentifier: MTPartyCancelCell.reuseIdentity(), for: indexPath) as! MTPartyCancelCell
            return cell
        }
    }
    
    func configureCell(cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
    }
    
    //MARK: UITableViewDelegate
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            if indexPath.row == 0 {
                self.delegate?.typeSelectDidRemoveFromSuperView(value: "商业任务")
            } else {
                self.delegate?.typeSelectDidRemoveFromSuperView(value: "个人任务")
            }
        } else {
            self.delegate?.typeSelectDidRemoveFromSuperView(value: "")
        }
        self.removeFromSuperview()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 1 ? 8 : CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}

class MTPartyTypeView: UIView {
    
    var delegate: MTPartyTypeViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        let tap = UITapGestureRecognizer(target: self, action: #selector(MTPartyTypeView.didTap(tap:)))
        tap.delegate = self
        self.addGestureRecognizer(tap)
        self.tableView.reloadData()
    }
            
    @objc private func didTap(tap: UITapGestureRecognizer) {
        self.frame = CGRect(x: 0, y: ScreenHeight, width: ScreenWidth, height: 300)
        
        self.removeFromSuperview()
        self.delegate?.typeSelectDidRemoveFromSuperView(value: "")
    }
    private func setupUI() {
        addSubview(bottomContentView)
        bottomContentView.addSubview(tableView)
    }
    
    private func makeConstraint() {
//        bottomContentView.snp.makeConstraints { (make) in
//            make.left.right.bottom.equalToSuperview()
//            make.height.equalTo(192)
//        }
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    lazy var bottomContentView: UIView = {
        let v = UIView()
        v.frame = CGRect(x: 0, y: ScreenHeight, width: ScreenWidth, height: ScreenHeight)
        return v
    }()
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        UIView.animate(withDuration: 0.25) {
            self.bottomContentView.frame = CGRect(x: 0, y: ScreenHeight - 192, width: ScreenWidth, height: ScreenHeight)
        }
    }
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: .zero, style: .grouped)
        table.delegate = self
        table.dataSource = self
        table.register(MTPartyCancelCell.self, forCellReuseIdentifier: MTPartyCancelCell.reuseIdentity())
        table.register(MTTypeExTableViewCell.self, forCellReuseIdentifier: MTTypeExTableViewCell.reuseIdentity())
        table.register(MTTypeTableViewCell.self, forCellReuseIdentifier: MTTypeTableViewCell.reuseIdentity())
        //table.separatorStyle = .none
        table.separatorInset = UIEdgeInsets.zero
        table.tableFooterView = UIView()
//        table.backgroundColor = .white
        table.isScrollEnabled = false
        return table
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}
